<template>
  <el-dialog :visible.sync="value" :before-close="cancel">
    <template slot="title">{{isCreated ? '添加' : '编辑'}}代充返利方案</template>
    <el-form :model="form" :rules="refRule" ref="refForm" :label-width="formLabelWidth">
      <el-form-item label="代充返利方案名" prop="name">
        <el-col :span="16">
          <el-input
            v-model="form.name"
            @keyup.enter.native="doSave({action: 'saveForm', form: 'refForm'})"
          ></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-col :span="16">
          <el-select v-model="form.status">
            <el-option
              v-for="(item,index) in consts.COMMISSION_STATUS"
              :label="item.title"
              :value="item.value"
              :key="index"
            ></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="代充手续费比例(%)" prop="feeRate">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="form.feeRate">
            <template slot="append">%</template>
          </el-input>
          <div class="tips">单笔代充手续费,用作代充返利计算</div>
        </el-col>
      </el-form-item>
      <el-form-item label="代充返利比例(%)" prop="commRate">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="form.commRate">
            <template slot="append">%</template>
          </el-input>
          <div class="tips">按照百分比比例返现代充手续费给代理商</div>
        </el-col>

      </el-form-item>
      <el-form-item label="返充返利结算模式" prop="settle">
        <el-col :span="8">
          <el-select v-model="form.settle">
            <el-option
              v-for="(item,index) in consts.COMMISSION_RATE_SETTLEMENT_MODE"
              :label="item.title"
              :value="item.value"
              :key="index"
            ></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="最低结算额度" prop="minSettleAmount">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="form.minSettleAmount">
            <template slot="append">元</template>
          </el-input>
          <div class="tips">每期代充返利结算金额下限,未达到则顺延到下一期结算</div>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="updatedPlan()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { form } from '@/build';
import { agentRechargeRebatePlansSave } from '@/services/api/commission';

export default {
  name: 'commission-config-dialog',
  props: ['value', 'id', 'rowData'],
  mixins: [form],
  data() {
    return {
      formLabelWidth: '180px',
      keywords: !this.id ? '新增' : '编辑',
      planName: undefined,
      form: {
        name: undefined,
        status: undefined,
        commRate: undefined,
        feeRate: undefined,
        minSettleAmount: undefined,
        ...this.rowData,
      },
      refRule: {
        name: [
          { required: true, message: '返充返利方案名不能为空' },
        ],
        status: [
          { required: true, message: '状态为必选项' },
        ],
        commRate: [
          { required: true, message: '必填项，请输入0~100之间的数字' },
        ],
        feeRate: [
          { required: true, message: '必填项，请输入0~100之间的数字' },
        ],
        minSettleAmount: [
          { required: true, message: '请输入大于等于0的数字', trigger: 'blur' },
        ],
      },
    };
  },
  computed: {
    isCreated() {
      return !this.id;
    },
  },
  mounted() {
    this.bindSave(agentRechargeRebatePlansSave, 'saveForm', 'form', {
      // to do
      afterSuccess: () => {
        this.cancel();
        this.$parent.getAll();
      },
    });
    this.planName = this.rowData.name;
  },
  methods: {
    cancel() {
      this.$emit('input', false);
    },
    updatedPlan() {
      if (this.planName === this.form.name) {
        delete this.form.name;
      }
      this.doSave({ action: 'saveForm', form: 'refForm' });
    },
  },
};
</script>

<style lang="scss" scoped>
.tips {
  font-size: 12px;
  color: #999;
  line-height: 1.5;
}
</style>
